from flask import request
from flaskapp.util.Redprint import Redprint
from flaskapp.util.Result import Result
from flaskapp.util.GPT import chat

api = Redprint('ai')

@api.route('/chat', methods=['POST'])
def ai_chat():
    """AI聊天接口"""
    data = request.get_json()
    content = data.get('content')
    
    if not content:
        return Result.error(400, '缺少必要参数')
    try:
        # 调用GPT接口获取回复
        response = chat(content)
        return Result.success({
            'content': response
        }, 'AI回复成功')
    except Exception as e:
        print(f"AI回复失败: {e}")
        return Result.error(500, f"AI回复失败: {str(e)}")